﻿using CoreShop.Library;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;

namespace CoreShop.App
{
    public partial class UITableItem : UIUserControl
    {
        public UITableItem()
        {
            InitializeComponent();

            Colors.Add(GlobalEnumVars.TableStatus.All, new TableColors(GlobalEnumVars.TableStatus.All, Color.FromArgb(34, 35, 51), Color.Black, Color.White));
            Colors.Add(GlobalEnumVars.TableStatus.Empty, new TableColors(GlobalEnumVars.TableStatus.Empty, Color.White, Color.FromArgb(243, 242, 239), Color.Black));
            Colors.Add(GlobalEnumVars.TableStatus.TobePlaced, new TableColors(GlobalEnumVars.TableStatus.TobePlaced, Color.FromArgb(28, 153, 69), Color.FromArgb(24, 130, 59), Color.White));
            Colors.Add(GlobalEnumVars.TableStatus.TobeSettled, new TableColors(GlobalEnumVars.TableStatus.TobeSettled, Color.FromArgb(58, 116, 194), Color.FromArgb(50, 101, 168), Color.White));
            Colors.Add(GlobalEnumVars.TableStatus.PreSettled, new TableColors(GlobalEnumVars.TableStatus.PreSettled, Color.Goldenrod, Color.FromArgb(226, 98, 13), Color.White));
            Colors.Add(GlobalEnumVars.TableStatus.Settlement, new TableColors(GlobalEnumVars.TableStatus.Settlement, Color.FromArgb(58, 116, 194), Color.FromArgb(50, 101, 168), Color.White));
            Colors.Add(GlobalEnumVars.TableStatus.TobeOperate, new TableColors(GlobalEnumVars.TableStatus.TobeOperate, Color.FromArgb(82, 82, 82), Color.FromArgb(103, 103, 103), Color.Yellow));
            Colors.Add(GlobalEnumVars.TableStatus.InUse, new TableColors(GlobalEnumVars.TableStatus.InUse, Color.FromArgb(123, 123, 123), Color.FromArgb(103, 103, 103), Color.Yellow));
        }

        public readonly Dictionary<GlobalEnumVars.TableStatus, TableColors> Colors = new Dictionary<GlobalEnumVars.TableStatus, TableColors>();

        private GlobalEnumVars.TableStatus tableStatus = GlobalEnumVars.TableStatus.Empty;

        public GlobalEnumVars.TableStatus TableStatus
        {
            get => tableStatus;
            set
            {
                tableStatus = value;
                Invalidate();
            }
        }

        private void UITableItem_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            var colors = Colors[TableStatus];
            RectColor = FillColor = colors.Color;
            //e.Graphics.FillRoundRectangle(colors.Color, new Rectangle(0, 0, Width - 1, Height - 1), 9);
            e.Graphics.FillRoundRectangle(colors.BottomColor, new Rectangle(0, Height - 40 - 1, Width - 1, 40), 9);
            e.Graphics.FillRectangle(colors.BottomColor, new Rectangle(1, Height - 40 - 1, Width - 2, 20));
            using (Font font = new Font("微软雅黑", 15))
            {
                e.Graphics.DrawString(TableName, font, colors.ForeColor, new Rectangle(12, 18, Width, Height), ContentAlignment.TopLeft);
            }

            if (tableStatus == GlobalEnumVars.TableStatus.TobeOperate)
            {
                using (Font font = new Font("微软雅黑", 18, System.Drawing.FontStyle.Bold))
                {
                    e.Graphics.DrawString(Money, font, colors.ForeColor, new Rectangle(20, 48, Width, Height), ContentAlignment.TopLeft);
                }
            }
            else
            {
                using (Font font = new Font("微软雅黑", 12))
                {
                    e.Graphics.DrawString(Money, font, colors.ForeColor, new Rectangle(12, 55, Width, Height), ContentAlignment.TopLeft);
                }
            }

            if (lblParent.ForeColor != colors.ForeColor)
                lblParent.ForeColor = lblDeskTime.ForeColor = lblParent.SymbolColor = lblDeskTime.SymbolColor = colors.ForeColor;
        }

        private string tableName = "";
        public string TableName
        {
            get => tableName;
            set
            {
                tableName = value;
                Invalidate();
            }
        }

        private string money = "";
        public string Money
        {
            get => money;
            set
            {
                money = value;
                Invalidate();
            }
        }

        public string Person
        {
            get => lblParent.Text;
            set => lblParent.Text = value;
        }

        public string Times
        {
            get => lblDeskTime.Text;
            set
            {
                lblDeskTime.Text = value;
                lblDeskTime.Visible = value.IsValid();
            }
        }

        public event EventHandler TableClick;

        private void UITableItem_Click(object sender, System.EventArgs e)
        {
            TableClick?.Invoke(this, e);
        }
    }
}
